import requests


class VmApi:

    def __init__(self, ip):
        self.ip = ip

    def get_ls_process_info(self):
        exec_text = """
        $processNames = "VDDMonitor", "gsmonitor", "lightstreamer", "linker", "gsm-assister", "watchdog", "PresentMon"
        Get-Process -Name $processNames | Select-Object -Property Id,Path,CommandLine,StartTime,ParentId
        """
        data = VmApi.base_func(self.ip, exec_text)
        check_list = ["VDDMonitor", "gsmonitor", "lightstreamer", "linker", "gsm-assister", "watchdog", "PresentMon"]

    def get_process_info(self):
        exec_text = """
        $processNames = "supervisord", "cginstanceagent", "task_manager", "RunBoostSvc", "cgvmagent", "svchost.", "ceph_game_patcher","cgvmmetrics"
        Get-Process -Name $processNames | Select-Object -Property Id,Path,CommandLine,StartTime,ParentId
        """
        VmApi.base_func(self.ip, exec_text)
    def get_task_manager_process_info(self):
        exec_text = """
        $processNames = "cgvmagent"
        Get-Process -Name $processNames | Select-Object -Property Id,Path,CommandLine,StartTime,ParentId
        """
        VmApi.base_func(self.ip, exec_text)
    def kill_process_svchost(self):
        exec_text = """
        taskkill -f /IM svchost.exe
        """
        VmApi.base_func(self.ip, exec_text)

    def kill_process_Brotato(self):
        exec_text = """
        taskkill -f /IM Brotato.exe
        """
        VmApi.base_func(self.ip, exec_text)
    def kill_process_ceph_game_patcher(self):
        exec_text = """
        taskkill -f /IM ceph_game_patcher.exe
        """
        VmApi.base_func(self.ip, exec_text)

    def tail_log(self):
        exec_text = 'Get-Content -Path "d:/CloudGameBundle/workspace/service-group.yaml"'
        VmApi.base_func(self.ip, exec_text)

    def find_item(self):
        exec_text = """
        Get-ChildItem -Path "C:/Users/CloudGame/AppData/Roaming\Microsoft/Windows/Start Menu/Programs/Startup/" -File
        """
        VmApi.base_func(self.ip, exec_text)

    @staticmethod
    def base_func(ip, text):
        url = f"http://{ip}:49997/v1/taskmanager/exec?shell=ps&output=plain"
        # url = f"http://{ip}:48090/v1/taskmanager/exec?shell=ps&output=plain"
        headers = {
            "Content-Type": "text/plain",
            "Accept": "text/plain"
        }
        resp = requests.post(url=url, data=text, headers=headers)
        # resp.encoding = 'UTF-8'
        # print(resp.encoding)
        # print(resp.text)
        data = resp.text
        print(data)
        return data

    def find_cginstanceagent_log(self):
        exec_text = 'Get-Content -Path "d:/CloudGameBundle/apps/cginstanceagent/current/logs/cginstanceagent.log"'
        VmApi.base_func(self.ip, exec_text)

    def find_cgvmagent_log(self):
        exec_text = 'Get-Content -Path "d:/cgvmagent/logs/cgvmagent.log" -Tail 100'
        VmApi.base_func(self.ip, exec_text)
    def find_task_manager_log(self):
        exec_text = 'Get-Content -Path "d:d:/CloudGameBundle/apps/task_manager/current/logs/task_manager.log"'
        VmApi.base_func(self.ip, exec_text)

    def stop_cg_vmagent(self):
        exec_text = 'supervisorctl.exe stop cgvmagent'
        VmApi.base_func(self.ip, exec_text)


    def start_cg_vmagent(self):
        exec_text = 'supervisorctl.exe start cgvmagent'
        VmApi.base_func(self.ip, exec_text)

    def find_cginstanceagent_conf(self):
        exec_text = 'Get-Content -Path "d:/CloudGameBundle/apps/cginstanceagent/current/configs/file/server.yaml"'
        VmApi.base_func(self.ip, exec_text)


    def get_path_process(self):
        exec_text = 'Get-Process -Name cginstanceagent | Select-Object -ExpandProperty Path'
        VmApi.base_func(self.ip, exec_text)

if __name__ == '__main__':
    vmClient = VmApi("10.54.65.105")
    # vmClient.get_process_info()
    # vmClient.tail_log()
    vmClient.find_cginstanceagent_log()
    # vmClient.kill_process_Brotato()
    # vmClient.stop_cg_vmagent()
    # vmClient.start_cg_vmagent()
    # vmClient.find_cginstanceagent_conf()
    # vmClient.kill_process_Brotato()
    # vmClient.find_task_manager_log()
